package com.euphoria.commands;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.facebook.android.Example;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import com.facebook.android.*;

public class RequestDetailsCommand extends BaseRequestListener {
	
	private IFacebookUIResponder _responder;
	
	public RequestDetailsCommand(IFacebookUIResponder responder) {
		_responder = responder;
	}	
    public void onComplete(final String response, final Object state) {
        try {
            // process the response here: executed in background thread
            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            final String name = json.getString("name");

            // then post the processed result back to the UI thread
            // if we do not do this, an runtime exception will be generated
            // e.g. "CalledFromWrongThreadException: Only the original
            // thread that created a view hierarchy can touch its views."
            /*Example.this.runOnUiThread(new Runnable() {
                public void run() {
                    mText.setText("Hello there, " + name + "!");
                }
            });*/
            _responder.handleResponse(json);            
        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }
    }
}
